package com.njtoyo.taxi.admin.service.local_service.commodity.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.library.common.OperationRecordHelper;
import com.njtoyo.taxi.admin.mapper.master.entity.local_service.LifeMerchantCommodity;
import com.njtoyo.taxi.admin.mapper.master.entity.local_service.LifeMerchantCommodityCategory;
import com.njtoyo.taxi.admin.mapper.master.mapper.local_service.LifeMerchantCommodityCategoryMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.local_service.LifeMerchantCommodityMapper;
import com.njtoyo.taxi.admin.rest.wrapper.local_service.platform.PlatformAddWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.local_service.platform.PlatformUpdateWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.local_service.platform.PlatformWrapper;
import com.njtoyo.taxi.admin.service.local_service.commodity.CategoryService;
import com.taxi.entity.common.RestResult;
import com.taxi.entity.common.ResultEnum;
import com.njtoyo.taxi.entity.backend.AdminUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static org.springframework.util.ObjectUtils.isEmpty;

@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private LifeMerchantCommodityCategoryMapper lifeMerchantCommodityCategoryMapper;

    @Autowired
    private LifeMerchantCommodityMapper lifeMerchantCommodityMapper;

    @Autowired
    private OperationRecordHelper operationRecordHelper;

    /**
     * 所有的分类
     *
     * @return
     */
    @Override
    public RestResult<List<LifeMerchantCommodityCategory>> all() {
        QueryWrapper<LifeMerchantCommodityCategory> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "commodity_exchange_type");
        List<LifeMerchantCommodityCategory> lifeMerchantCommodityCategories =
                lifeMerchantCommodityCategoryMapper.selectList(wrapper);
        return RestResult.success(lifeMerchantCommodityCategories);
    }

    /**
     * 商品分类列表
     *
     * @param adminUser
     * @param wrapper
     * @return
     */
    @Override
    public RestResult<Page<LifeMerchantCommodityCategory>> getPage(AdminUser adminUser, PlatformWrapper wrapper) {
        switch (adminUser.getAccountType()) {
            case platform:
                break;
            default:
                return RestResult.failed("账号权限不够");
        }
        Page<LifeMerchantCommodityCategory> page = lifeMerchantCommodityCategoryMapper.getPage(wrapper);
        return RestResult.success(page);
    }

    /**
     * 商品分类添加
     *
     * @param adminUser
     * @param wrapper
     * @return
     */
    @Override
    public RestResult add(AdminUser adminUser, PlatformAddWrapper wrapper) {
        switch (adminUser.getAccountType()) {
            case platform:
                break;
            default:
                return RestResult.failed("账号权限不够");
        }
        LifeMerchantCommodityCategory lifeMerchantCommodityCategory = new LifeMerchantCommodityCategory();
        BeanUtils.copyProperties(wrapper, lifeMerchantCommodityCategory);
        lifeMerchantCommodityCategoryMapper.insert(lifeMerchantCommodityCategory);
        Long id = lifeMerchantCommodityCategory.getId();
        operationRecordHelper.addBusinessLog(String.format("新增了商品商品分类 [%d] 的信息", id));
        return RestResult.success();
    }

    /**
     * 商品分类修改
     *
     * @param id
     * @param wrapper
     * @return
     */
    @Override
    public RestResult update(Long id, PlatformUpdateWrapper wrapper) {
        LifeMerchantCommodityCategory lifeMerchantCommodityCategory = lifeMerchantCommodityCategoryMapper.selectById(id);
        if (isEmpty(lifeMerchantCommodityCategory)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }

        new LambdaUpdateChainWrapper<>(lifeMerchantCommodityCategoryMapper)
                .eq(LifeMerchantCommodityCategory::getId, id)
                .set(LifeMerchantCommodityCategory::getName, wrapper.getName())
                .set(LifeMerchantCommodityCategory::getCommodityExchangeType, wrapper.getCommodityExchangeType())
                .set(LifeMerchantCommodityCategory::getState, wrapper.getState())
                .set(LifeMerchantCommodityCategory::getSortBy, wrapper.getSortBy())
                .set(LifeMerchantCommodityCategory::getUpdatedAt, new Date())
                .update();
        operationRecordHelper.addBusinessLog(String.format("修改了商品商品分类 [%d] 的信息", id));
        return RestResult.success();
    }

    /**
     * 商品分类删除
     *
     * @param id
     * @return
     */
    @Override
    public RestResult delete(Long id) {
        LifeMerchantCommodityCategory lifeMerchantCommodityCategory = lifeMerchantCommodityCategoryMapper.selectById(id);
        if (isEmpty(lifeMerchantCommodityCategory)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        //类型下存在商品无法删除
        QueryWrapper<LifeMerchantCommodity> lifeMerchantCommodityWrapper = new QueryWrapper<>();
        lifeMerchantCommodityWrapper.eq("commodity_category_id", id);
        Integer integer = lifeMerchantCommodityMapper.selectCount(lifeMerchantCommodityWrapper);
        if (integer > 0) {
            return RestResult.failed("商品类型下存在商品");
        }

        //删除
        QueryWrapper<LifeMerchantCommodityCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        lifeMerchantCommodityCategoryMapper.delete(wrapper);
        operationRecordHelper.addBusinessLog(String.format("删除了商品分类 [%d] 的信息", id));
        return RestResult.success();
    }


}
